package com.jinlongchen.api;

import com.jinlongchen.util.DBUtil;
import com.jinlongchen.model.Album;
import com.jinlongchen.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/api/my-album-list.json")
public class MyAlbumListApi extends AbsApiServlet {
    @Override
    protected Object doGetInner(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException, APIException {
        User currentUser =(User) req.getSession().getAttribute("currentUser");
        if (currentUser == null){
            throw new APIException("用户必须首先进行登录");
        }

        List<Album> albumList = new ArrayList<>();

        try (Connection c = DBUtil.getConnection()) {
            String sql = "SELECT aid,name,cover,count FROM album WHERE uid = ? ORDER BY aid DESC";
            try (PreparedStatement s = c.prepareStatement(sql)) {
                s.setInt(1,currentUser.uid);
                try (ResultSet rs = s.executeQuery()) {
                    while (rs.next()) {
                        Album album = new Album();
                        album.aid = rs.getInt("aid");
                        album.name = rs.getString("name");
                        album.cover = rs.getString("cover");
                        album.count = rs.getInt("count");

                        albumList.add(album);
                    }
                }
            }
        }
        return albumList;
    }


//    private final ObjectMapper objectMapper = new ObjectMapper();
//    @Override
//    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //1、根据用户有没有传 keyword 做不同的处理
//        req.setCharacterEncoding("utf-8");
//        String keyword = req.getParameter("keyword");
//        if (keyword != null && keyword.trim().isEmpty()){
//            keyword = null;
//        }
//
//        //问题：每次返回所有的专辑，当专辑数量变多时，这种实现非常不合适
//        List<Album> albumList = new ArrayList<>();
//        Object result;
//        //从MySQL中拿到所有的专辑列表
//        try (Connection c = DBUtil.getConnection()){
//            String sql;
//            if (keyword == null) {
//                sql = "SELECT aid,name,cover,count FROM album ORDER BY aid";
//            }else {
//                sql = "SELECT aid,name,cover,count FROM album WHERE name LIKE ? ORDER BY aid";
//            }
//            try (PreparedStatement s = c.prepareStatement(sql)){
//                if (keyword != null){
//                    s.setString(1,"%" + keyword + "%");
//                }
//                try (ResultSet rs = s.executeQuery()){
//                    while (rs.next()){
//                        Album album = new Album();
//                        album.aid = rs.getInt("aid");
//                        album.name = rs.getString("name");
//                        album.cover = rs.getString("cover");
//                        album.count = rs.getInt("count");
//
//                        albumList.add(album);
//                    }
//                }
//            }
//            result = new Object(){
//                public boolean success = true;
//                public Object data = albumList;
//            };
//        }catch (SQLException exc){
//            //1、打印异常栈，便于找到出错原因
//            exc.printStackTrace();
//            //2、把异常中的消息作为错误原因写到API中
//            result = new Object(){
//                public boolean success = false;
//                public String reason = exc.getMessage();
//            };
//        }
//
//
//        String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(result);
//
//        resp.setCharacterEncoding("utf-8");
//        resp.setContentType("application/json");
//        resp.getWriter().println(json);
//    }
}
